package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.BedVo;
import com.zzyl.nursing.vo.DeviceInfo;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    /**
     * 根据房间id查询房间数据(楼层、房间、价格)
     *
     * @param id
     * @return
     */
    @Override
    public RoomVo getRoomById(Long id) {
        return roomMapper.selectRoomInfoById(id);
    }

    /**
     * 获取房间中的智能设备及数据
     *
     * @param floorId
     */
    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        //1、查询楼层下的房间、房间的床、老人、设备
        List<RoomVo> roomVoList = roomMapper.selectRoomsWithDeviceByFloorId(floorId);
        System.out.println("roomVoList = " + roomVoList);
        //2、处理设备的最新数据
        if (CollUtil.isNotEmpty(roomVoList)) {
            //2.1 遍历房间里的设备
            for (RoomVo roomVo : roomVoList) {
                List<DeviceInfo> roomDeviceVos = roomVo.getDeviceVos();
                if (CollUtil.isNotEmpty(roomDeviceVos)) {
                    for (DeviceInfo roomDeviceVo : roomDeviceVos) {
                        //2.3 获取设备id
                        String iotId = roomDeviceVo.getIotId();
                        //2.4 获取设备最新的数据
                        String deviceDataJsonString = (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, iotId);
                        //2.5 把最新的数据存入vo中
                        roomDeviceVo.setDeviceDataVos(JSONUtil.toList(deviceDataJsonString, DeviceData.class));
                    }
                }
                //3、床位上的设备数据
                List<BedVo> bedVoList = roomVo.getBedVoList();
                if (CollUtil.isNotEmpty(bedVoList)) {
                    ////3.1 遍历房间里的床位
                    for (BedVo bedVo : bedVoList) {
                        //3.2 获取床位上的设备
                        List<DeviceInfo> bedDeviceVos = bedVo.getDeviceVos();
                        if (CollUtil.isNotEmpty(bedDeviceVos)) {
                            //3.3遍历床位上的设备
                            for (DeviceInfo bedDeviceVo : bedDeviceVos) {
                                //3.4 获取设备id
                                String iotId = bedDeviceVo.getIotId();
                                //3.5 通过设备id查询redis中这个设备最新的数据
                                String deviceDataJsonString = (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, iotId);
                                //3.6 把最新的数据存入vo中
                                bedDeviceVo.setDeviceDataVos(JSONUtil.toList(deviceDataJsonString, DeviceData.class));
                            }
                        }
                    }
                }

            }
            return roomVoList;
            //4 返回
        }
        return List.of();
    }

}
